package com.qf.petmall.starter.coupon.controller;


import com.qf.petmall.starter.coupon.entity.Coupon;
import com.qf.petmall.starter.coupon.service.ICouponService;
import com.qf.petmall.starter.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 优惠券模块 前端控制器
 * </p>
 *
 * @author Zhong_
 * @since 2022-12-06
 */
@CrossOrigin
@RestController
@RequestMapping("/coupon")
public class CouponController {

    @Autowired
    ICouponService couponService;

    @GetMapping("/select")
    public R getCoupon(){

        List<Coupon> list = couponService.list();

        return R.ok().put("coupon",list);
    }

    @PostMapping("/update")
    public R update(@RequestBody Coupon coupon){

        Coupon byId = couponService.getById(coupon.getId());
        byId.setHidden(!byId.isHidden());
        couponService.updateById(byId);

        return R.ok().put("coupon",byId);
    }

    @GetMapping("/delete/{id}")
    public R deleteCoupon(@PathVariable("id") Integer id){

        couponService.removeById(id);

        return R.ok().put("code",200);
    }

    @PostMapping("/add")
    public R addCoupon(@RequestBody Coupon coupon){

        couponService.save(coupon);

        return R.ok().put("code",200);
    }
}
